from django.http import JsonResponse


class ResultCode(object):
    """
    成功 ==> 200
    参数错误 ==> 400
    没权限 ==> 401
    方法错误 ==> 405
    服务器错误 ==> 500
    """
    ok_code = 200
    params_error_code = 400
    auth_error_code = 401
    method_error_code = 405
    serve_error_code = 500

    @classmethod
    def _result_method(cls, code, msg=None, data=None, **kwargs):
        """
        Ajax请求返回JSON
        :param code:
            状态码
        :param msg:
            信息
        :param data:
            数据
        :param kwargs:
        :return:
        """
        _result = dict(
            code=code,
            msg=msg,
            data=data,
        )
        if kwargs and isinstance(kwargs, dict) and kwargs.keys():
            _result.update(kwargs)
        return JsonResponse(_result)

    @classmethod
    def ok(cls, msg="成功~~", data=None, **kwargs):
        return cls._result_method(cls.ok_code, msg=msg, data=data, **kwargs)

    @classmethod
    def params_error(cls, msg="参数错误~~", data=None, **kwargs):
        return cls._result_method(cls.params_error_code, msg=msg, data=data, **kwargs)

    @classmethod
    def auth_error(cls, msg="未授权~~", data=None, **kwargs):
        return cls._result_method(cls.auth_error_code, msg=msg, data=data, **kwargs)

    @classmethod
    def method_error(cls, msg="方法错误~~", data=None, **kwargs):
        return cls._result_method(cls.method_error_code, msg=msg, data=data, **kwargs)

    @classmethod
    def serve_error(cls, msg="服务器错误~~", data=None, **kwargs):
        return cls._result_method(cls.serve_error_code, msg=msg, data=data, **kwargs)
